<template>
  <div class="app-container">
    <el-form :model="queryParam" ref="queryForm" :inline="true">
      <el-form-item label="提问者用户名：">
        <el-input v-model="queryParam.sendUserName"></el-input>
      </el-form-item>
      <el-form-item label="提问内容：">
        <el-input v-model="queryParam.publishContent"></el-input>
      </el-form-item>
      <el-form-item label="学科：" >
        <el-select v-model="queryParam.subjectId"  clearable>
          <el-option v-for="item in subjectLevel1" :key="item.id" :value="item.id" :label="item.categoryName" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="listLoading" :data="tableData" border fit highlight-current-row style="width: 100%">
      <el-table-column prop="messageId" label="Id"  width="100" />
      <el-table-column prop="publishContent" label="内容" show-overflow-tooltip />
      <el-table-column prop="userName" label="提问人"  width="100" />
      <el-table-column prop="likes" label="点赞数" width="70" />
      <el-table-column prop="createTime" label="创建时间" width="160px"/>
      <el-table-column  label="操作" align="left"  width="260px">
        <template slot-scope="{row}">
          <!-- 编辑按钮，点击后通过路由跳转到编辑页面，携带 id 参数 -->
          <el-button type="primary" size="mini" @click="$router.push({path:'/message/detail',query:{id:row.messageId}})" >查看回复</el-button>
          <el-button type="success" v-if="row.replyCount==0" size="mini" @click="$router.push({path:'/message/send',query:{id:row.messageId}})" >回    复</el-button>

        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParam.pageIndex" :limit.sync="queryParam.pageSize"
                @pagination="search"/>
  </div>
</template>

<script>

import Pagination from '@/components/Pagination'
import messageApi from '@/api/message'
import subjectApi from '@/api/subject.js'

export default {
  components: { Pagination },
  data () {
    return {
      queryParam: {
        messageId: null,
        userName: null,
        publishContent:null,
        pageIndex: 1,
        pageSize: 10
      },
      listLoading: true,
      tableData: [],
      total: 0,
      subjectLevel1:[]
    }
  },
  created () {
    this.search()
    this.initSubject()
  },
  methods: {
    initSubject(){
      subjectApi.selectLevel1().then(data=>{
        console.log(data)
        const re = data.response
        this.subjectLevel1=re
      })
    },
    search () {
      this.listLoading = true
      messageApi.pageList(this.queryParam).then(data => {
        const re = data.response
        this.tableData = re.list
        this.total = re.total
        this.queryParam.pageIndex = re.pageNum
        this.listLoading = false
      })
    },
    submitForm () {
      this.queryParam.pageIndex = 1
      this.search()
    }
  }
}
</script>
